package com.kuzlak.cms.module.sys.controller;


import com.kuzlak.cms.bean.MyPage;
import com.kuzlak.cms.bean.Result;
import com.kuzlak.cms.constant.ResultConstant;
import com.kuzlak.cms.module.sys.service.SysExceptionService;
import com.kuzlak.cms.module.sys.service.SysLoginLogService;
import com.kuzlak.cms.module.sys.service.SysOperationLogService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Hesenjan(Serkol)
 * @since 2021-12-03
 */
@Tag(name = "日志管理")
@RestController
@RequestMapping("/api/operation")
public class OperationLogController {
    @Autowired
    private SysOperationLogService service;

    @Autowired
    private SysLoginLogService logService;

    @Autowired
    private SysExceptionService sysException;

    @Operation(summary= "操作日志")
    @PostMapping("/list")
    public Result list(@RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "pagesize", defaultValue = "10") int pagesize,
                       @Parameter(description= "开始时间") @RequestParam(name = "startDate", defaultValue = "") String startDate,
                       @Parameter(description= "结束时间") @RequestParam(name = "endDate", defaultValue = "") String endDate,
                       @Parameter(description= "系统模块") @RequestParam(name = "requestUrl", defaultValue = "") String requestUrl,
                       @Parameter(description= "操作类型") @RequestParam(name = "type", defaultValue = "0") int type,
                       @Parameter(description= "请求方式") @RequestParam(name = "requestMode", defaultValue = "") String requestMode,
                       @Parameter(description= "状态") @RequestParam(name = "status", defaultValue = "0") int status) {
        Result gridResult = new Result();
        MyPage page1 = service.findByPage(pagesize, page, type, status, startDate, endDate, requestUrl, requestMode);


        gridResult.setData(page1);
        gridResult.setCode(ResultConstant.SUCSESS);

        return gridResult;
    }

    @Operation(summary= "登录日志")
    @PostMapping("/loginList")
    public Result loginList(@RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "pagesize", defaultValue = "10") int pagesize,
                            @Parameter(description= "开始时间") @RequestParam(name = "startDate", defaultValue = "") String startDate,
                            @Parameter(description= "结束时间") @RequestParam(name = "endDate", defaultValue = "") String endDate,
                            @Parameter(description= "登录人员") @RequestParam(name = "userName", defaultValue = "") String userName,
                            @Parameter(description= "操作类型") @RequestParam(name = "ip", defaultValue = "") String ip,
                            @Parameter(description= "请求方式") @RequestParam(name = "address", defaultValue = "") String address,
                            @Parameter(description= "状态") @RequestParam(name = "state", defaultValue = "-1") int state) {
        Result gridResult = new Result();
        MyPage page1 = logService.findByPage(pagesize, page, state, startDate, endDate, userName, ip, address);


        gridResult.setData(page1);
        gridResult.setCode(ResultConstant.SUCSESS);

        return gridResult;
    }


    @Operation(summary= "系统异常")
    @PostMapping("/syslist")
    public Result syslist(@RequestParam(name = "page", defaultValue = "0") int page,
                          @RequestParam(name = "pagesize", defaultValue = "10") int pagesize,
                          @Parameter(description= "开始时间") @RequestParam(name = "startDate", defaultValue = "") String startDate,
                          @Parameter(description= "结束时间") @RequestParam(name = "endDate", defaultValue = "") String endDate,
                          @Parameter(description= "系统模块") @RequestParam(name = "name", defaultValue = "") String name,
                          @Parameter(description= "操作类型") @RequestParam(name = "messeges", defaultValue = "") String messeges,
                          @Parameter(description= "请求方式") @RequestParam(name = "packageName", defaultValue = "") String packageName,
                          @Parameter(description= "状态") @RequestParam(name = "code", defaultValue = "") String code,
                          @Parameter(description= "状态") @RequestParam(name = "userName", defaultValue = "") String userName) {
        Result gridResult = new Result();
        MyPage page1 = sysException.findByPage(pagesize, page, startDate, endDate, name, messeges, packageName, code, userName);


        gridResult.setData(page1);
        gridResult.setCode(ResultConstant.SUCSESS);

        return gridResult;
    }

    @Operation(summary= "退出登录")
    @PostMapping("/logout")
    public Result logout(       @Parameter(description= "用户编号") @RequestParam(name = "userId", defaultValue = "0") long userId) {
        Result result = new Result();

        boolean remove = logService.clearToken(userId);
        if (remove) {
            result.setMsg("退出成功");
            result.setCode(ResultConstant.SUCSESS);
        } else {
            result.setMsg("退出失败");
            result.setCode(ResultConstant.FAIL);
        }

        return result;

    }

}

